using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CXMLStudioIDE.WinFormsUI.Docking;
using CXMLStudioIDE.Common;
using CXMLStudioIDE.Common.Entities;
using CXMLStudioIDE.Views;
using CXMLStudioIDE.Presenters;

namespace CXMLStudioIDE.Forms
{
    public partial class SolutionExplorerForm : ToolWindowForm , ISolutionExplorerView
    {
        public int i_SelectedNodeIndex;
        private SolutionExplorerPresenter m_presenter;

        public SolutionExplorerForm(MainForm parent)
        {
            ParentMainForm = parent;
            InitializeComponent();
            m_presenter = new SolutionExplorerPresenter(this);
            this.menuItemAddNewProject.Click += OnAddNewProject;
            this.menuItemAddExProject.Click += OnAddExistingProject;
            this.menuItemAddExBE.Click += OnAddExistingBE;
            this.menuItemAddNewBE.Click += OnAddNewBE;
            this.menuItemRemove.Click += OnRemove;
            this.menuItemProps.Click += OnPropertiesClick;
            this.treeView1.NodeMouseDoubleClick += OnNodeMouseClick;
            this.treeView1.KeyPress += MyOnKeyPress;
        }

        protected override void OnRightToLeftLayoutChanged(EventArgs e)
        {
            treeView1.RightToLeftLayout = RightToLeftLayout;
        }

        public void ClearTreeView()
        {
            this.treeView1.Nodes.Clear();
        }

        private void OnMouseUp(object sender, MouseEventArgs e)
        {
            string sZ_SelectedNodeText = "";
            if (e.Button == MouseButtons.Right)
            {
                this.treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y);
                sZ_SelectedNodeText = this.treeView1.SelectedNode.Text;
                if (this.treeView1.SelectedNode != null)
                {
                    if (sZ_SelectedNodeText.StartsWith("Solution"))
                    {
                        menuItemAdd.Visible = true;
                        ShowAddProjectMenuItems(true);
                        ShowAddEntityMenuItems(false);
                        menuItemProps.Visible = true;
                        toolSeparator1.Visible = true;
                        menuItemRemove.Visible = false;
                    }
                    if (sZ_SelectedNodeText.StartsWith("Project"))
                    {
                        menuItemProps.Visible = true;
                        toolSeparator1.Visible = true;
                        menuItemRemove.Visible = true;
                        ShowAddProjectMenuItems(false);
                    }
                    if (sZ_SelectedNodeText.StartsWith("Business Entities"))
                    {
                        menuItemAdd.Visible = true;
                        ShowAddEntityMenuItems(true);
                        ShowAddProjectMenuItems(false);
                        menuItemProps.Visible = false;
                        toolSeparator1.Visible = false;
                        menuItemRemove.Visible = false;
                    }
                    if (!sZ_SelectedNodeText.StartsWith("Solution") &&
                        !sZ_SelectedNodeText.StartsWith("Project") &&
                        !sZ_SelectedNodeText.StartsWith("Business Entities"))
                    {
                        menuItemAdd.Visible = false;
                        ShowAddEntityMenuItems(false);
                        ShowAddProjectMenuItems(false);
                        menuItemProps.Visible = false;
                        toolSeparator1.Visible = false;
                        menuItemRemove.Visible = true;
                    }

                }
            }
        }

        private void ShowAddProjectMenuItems(bool show)
        {
            menuItemAddProject.Visible = show;
            menuItemAddNewProject.Visible = show;
            menuItemAddExProject.Visible = show;
        }

        private void ShowAddEntityMenuItems(bool show)
        {
            menuItemAddBE.Visible = show;
            menuItemAddNewBE.Visible = show;
            menuItemAddExBE.Visible = show;
        }

        #region IProjectExplorerView Members

        #region IProjectExplorerView Public Properties

        public new MainForm ParentMainForm { get; private set; }

        public int SelectedNodeIndex
        {
            get
            {
                return this.treeView1.SelectedNode.Index;
            }
        }
        public TreeNode SelectedNode
        {
            get 
            {
                return this.treeView1.SelectedNode;
            }
        }
        public TreeNodeCollection Nodes
        {
            get
            {
                return this.treeView1.Nodes;
            }
        }

        public string SelectedNodeText
        {
            get
            {
                return this.treeView1.SelectedNode.Text;
            }
        }

        public string SelectedNodeName
        {
            get
            {
                return this.treeView1.SelectedNode.Name;
            }
        }

        #endregion

        #region IProjectExplorerView Methods

        public void PopulateTreeView(int parentID, DataTable projects, Solution solution)
        {
            m_presenter.PopulateTreeView(parentID, projects, solution);
        }

        public void ExpandAllTreeView()
        {
            this.treeView1.ExpandAll();
        }

        public void ClearNodes()
        {
            this.treeView1.Nodes.Clear();
        }

        #endregion

        #region IProjectExplorerView Events
        public event TreeNodeMouseClickEventHandler OnNodeMouseClick;
        public event EventHandler OnAddNewProject;
        public event EventHandler OnAddExistingProject;
        public event EventHandler OnRemove;
        public event EventHandler OnAddExistingBE;
        public event EventHandler OnAddNewBE;
        public event EventHandler OnPropertiesClick;
        public event KeyPressEventHandler MyOnKeyPress;
        #endregion

 
        #endregion
    }
}